//
//  NSObject+ModelCaches.m
//  FinancialStore
//
//  Created by 秦茂军 on 16/8/19.
//  Copyright © 2016年 zhenqiu. All rights reserved.
//

#import "NSObject+ModelCaches.h"
#import <objc/message.h>
#import "CacheHelper.h"
#import "MJExtension.h"

@implementation NSObject(ModelCaches)


/**缓存数据*/
+ (void)cacheData:(id)cacheData withCacheKey:(NSString *)cacheKey{
    if(!cacheKey||cacheKey.length ==0){
        GYNLog(@"缓存key不能为空");
        return;
    }
    if(!cacheData) {
        GYNLog(@"缓存数据为空");
        return;
    };
    //打成二进制更保险
    NSData * archiveData = [NSKeyedArchiver archivedDataWithRootObject:cacheData];
    if(archiveData.length>0){
        [[CacheHelper getInstance] setCacheValue:archiveData forKey:cacheKey];
    }
}

/**取本地缓存数据*/
+ (id )getCacheDataWithKey:(NSString *)cacheKey{
    NSData * archiveData = [[CacheHelper getInstance] getCacheValue:cacheKey];
    if([archiveData isKindOfClass:[NSData class]]){
        if(archiveData.length == 0){
            GYNLog(@"获取key为:%@ 的缓存不存在",cacheKey);
            return nil;
        }
        id objc = [NSKeyedUnarchiver unarchiveObjectWithData:archiveData];
        return objc;
    }else if ([archiveData isKindOfClass:[NSDictionary class]]){
        return archiveData;
    }else{
        return nil;
    }
}

@end
